package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.entity.enums.eTradeWarBattleType;
import com.yanqu.road.entity.tradewar.TradeWarWanted;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.tradewar.TradeWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarActivity;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_TRADE_WAR_TO_ENEMY_BATTLE, desc = "跨服商战到对手区服战斗")
public class CrossTradeWarToEnemyBattleCmd extends CrossUnionCommand {
    private CrossTradeWarProto.CrossTradeWarToEnemyBattleReqMsg reqMsg;
    private int activityId;
    private long enemyUserId;
    private long enemyServerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossTradeWarProto.CrossTradeWarToEnemyBattleReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        enemyUserId = reqMsg.getEnemyUserId();
        enemyServerId = reqMsg.getEnemyServerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YanQuMessage pbMsg;
        if(reqMsg.getBattleType() == eTradeWarBattleType.Track.getValue()){//追缉的
            int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, enemyUserId);
            if (groupId == UnionActivityGroupHelper.NO_GROUP_ID){
                TradeWarProto.TradeWarBattleStartRespMsg.Builder respMsg = TradeWarProto.TradeWarBattleStartRespMsg.newBuilder();
                respMsg.setRet(GameErrorCode.E_TRADE_WAR_NO_PREPARE);
                pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_TRADE_WAR_BATTLE_START, respMsg);
                MessageHelper.sendPacket(reqMsg.getMyServerId(), reqMsg.getMyUserId(), pbMsg);
                return;
            }

            CrossTradeWarActivity activity = CrossTradeWarMgr.getActivity(activityId, groupId);
            if (activity == null){
                TradeWarProto.TradeWarBattleStartRespMsg.Builder respMsg = TradeWarProto.TradeWarBattleStartRespMsg.newBuilder();
                respMsg.setRet(GameErrorCode.E_UNION_ACTIVITY_GROUP_WAIT_GROUP);
                pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_TRADE_WAR_BATTLE_START, respMsg);
                MessageHelper.sendPacket(reqMsg.getMyServerId(), reqMsg.getMyUserId(), pbMsg);
                return;

            }
            TradeWarWanted tradeWarWanted =  activity.getTradeWarWanted(reqMsg.getBattleUid());

            boolean isWantedOverTime;
            isWantedOverTime = !activity.isWantedTime(tradeWarWanted);
            if(activity.isRepeatTrack(reqMsg.getBattleUid(), reqMsg.getMyUserId())){//重复追缉
                TradeWarProto.TradeWarBattleStartRespMsg.Builder respMsg = TradeWarProto.TradeWarBattleStartRespMsg.newBuilder();
                respMsg.setRet(GameErrorCode.E_TRADE_WAR_NO_PREPARE);
                pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_CROSS_TRADE_WAR_BATTLE_START, respMsg);
                MessageHelper.sendPacket(reqMsg.getMyServerId(), reqMsg.getMyUserId(), pbMsg);
                return;
            }
            CrossTradeWarProto.CrossTradeWarToEnemyBattleReqMsg.Builder msg = reqMsg.toBuilder();
            msg.setIsWantedOverTime(isWantedOverTime);
            if(null != tradeWarWanted){
                if(tradeWarWanted.getUseItemId() == 0){
                    msg.setWantedItemId(GameConfig.TRADE_WAR_WANTED_ITEM_ID);
                }else {
                    msg.setWantedItemId(tradeWarWanted.getUseItemId());
                }
            }
            pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_TO_ENEMY_BATTLE, msg);
        }else {
            pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_TO_ENEMY_BATTLE, reqMsg);
        }
        MessageHelper.sendPacket(enemyServerId, enemyUserId, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
